package net.boar.os;

import java.io.*;

/**
 * @Author: zgl
 * @Date: 2021/6/15 16:30
 * @Description:
 */
public class Build {

    public Build() {
    }


    public static void builder(WorkerEntity entity) {
        excuteCMDBatFile(entity);
    }

    private static boolean writeFile(File exportFile, String content) {
        if (exportFile != null && content != null && content.length() != 0) {
            if (!exportFile.exists()) {
                try {
                    exportFile.getParentFile().mkdirs();
                    exportFile.createNewFile();
                } catch (IOException var21) {
                    var21.printStackTrace();
                    return false;
                }
            }

            BufferedWriter bufferedWriter = null;

            boolean var4;
            try {
                FileOutputStream os = new FileOutputStream(exportFile);
                FileDescriptor fd = os.getFD();
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                bufferedWriter.write(content);
                bufferedWriter.flush();
                fd.sync();
                return true;
            } catch (UnsupportedEncodingException var22) {
                var22.printStackTrace();
                var4 = false;
            } catch (FileNotFoundException var23) {
                var23.printStackTrace();
                var4 = false;
                return var4;
            } catch (IOException var24) {
                var24.printStackTrace();
                var4 = false;
                return var4;
            } catch (Exception var25) {
                var25.printStackTrace();
                var4 = false;
                return var4;
            } finally {
                try {
                    if (bufferedWriter != null) {
                        bufferedWriter.close();
                        bufferedWriter = null;
                    }
                } catch (IOException var20) {
                    var20.printStackTrace();
                }

            }

            return var4;
        } else {
            return false;
        }
    }

    private static boolean excuteCMDBatFile(WorkerEntity entity) {
        String METHOD_NAME = "excuteCMDBatFile#";
        boolean result = true;
        File batFile = new File(entity.getWorkerPath() + entity.getCreateBarName());
        System.out.println(batFile.getAbsolutePath());
        boolean isSuccess = writeFile(batFile, entity.getCmd());
        if (!isSuccess) {
            System.out.println("excuteCMDBatFile#write cmd to File failed.");
            return false;
        } else {
            System.out.println("工作目录:" + entity.getWorkerPath());

            try {
                Process p = Runtime.getRuntime().exec(entity.getWorkerPath() + entity.getCreateBarName(), (String[])null, new File(entity.getWorkerPath()));
                InputStream fis = p.getErrorStream();
                InputStreamReader isr = new InputStreamReader(fis, entity.getEncoding());
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                StringBuilder builder = new StringBuilder();

                while((line = br.readLine()) != null) {
                    builder.append(line);
                }

                p.waitFor();
                int i = p.exitValue();
                System.out.println("excuteCMDBatFile#exitValue = " + i);
                if (i != 0) {
                    result = false;
                    System.out.println("excuteCMDBatFile#excute cmd failed, [result = " + result + ", error message = " + builder.toString() + "]");
                } else {
                    System.out.println("excuteCMDBatFile#excute cmd result = " + result);
                }
            } catch (Exception var12) {
                result = false;
                var12.printStackTrace();
            }

            return result;
        }
    }
}
